layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var DT = {
        tableId: "dataTable"    //表格id
    };

    DT.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(DT.tableId, {where: queryData});
    };


    DT.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'name', sort: false, title: '名称'},
            {field: 'settingKey', sort: false, title: '键'},
            {field: 'settingVal', sort: false, title: '值'},
            {
                field: 'clzType', sort: false, title: '类型', templet: function (d) {
                    if (d.clzType ==1) {
                        return '<span class="layui-badge layui-bg-orange">系统</span>';
                    } else {
                        return '<span class="layui-badge layui-bg-gray">自定义</span>';
                    }
                }
            },
            {field: 'memo', sort: false, false: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    DT.openAdd = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: ['700px','350px'],
            title: '新增配置',
            content: Sam.ctxPath + '/sam/settings/add',
            end: function () {
                admin.getTempData('formOk') && DT.search();
            }
        });
    };

    DT.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: ['700px','350px'],
            title: '修改配置',
            content: Sam.ctxPath + '/sam/settings/edit/' + data.id,
            end: function () {
                admin.getTempData('formOk') && DT.search();
            }
        });
    };

    DT.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(Sam.ctxPath + "/sam/settings/delete/"+data.id, function () {
                Sam.success("删除配置成功!");
                DT.search();
            }, function (data) {
                Sam.error("删除配置失败!" + data.responseJSON.message + "!");
            });
            ajax.start();
        };
        Sam.confirm("是否删除该配置?", operation);
    };

    var tableResult = table.render({
        elem: '#' + DT.tableId,
        url: Sam.ctxPath + '/sam/settings/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: DT.initColumn()
    });

    $('#btnSearch').click(function () {
        DT.search();
    });

    $('#btnAdd').click(function () {
        DT.openAdd();
    });

    table.on('tool(' + DT.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DT.onEdit(data);
        } else if (layEvent === 'delete') {
            DT.onDelete(data);
        }
    });
});
